<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 添加接口
 *
 * @author      Eden
 * @datetime    2016/12/1 22:56
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
class Express_Model extends API_Model
{

    // 参数
    private $params;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     * @param array $params
     */
    public function run(Array $params)
    {
        $this->params = $params;
        $send['state'] = false;
        do {
            if (empty($this->params['orderId'])) {
                $send['error'] = '参数错误';
                break;
            }

            // 查询订单号
            $query = $this->db->get_where('user_order_express', array('orderId' => $params['orderId']));
            $express = $query->row_array();

            if (empty($express)) {
                $send['state'] = true;
                $send['data'] = new stdClass();
                break;
            }

            $this->load->library('util/Util_AliExpress', null, 'AliExpress');
            $res = $this->AliExpress->ask($express['simpleName'], $express['expressNo']);

            if ($res && is_array($res)) {
                $result = value($res, 'showapi_res_body');
                if (!empty($result) && isset($result['ret_code']) && $result['ret_code'] == 0) {
                    // 成功
                    $send['state'] = true;
                    $send['data'] = array(
                        'expressNo' => $express['expressNo'],
                        'update' => $result['update'],
                        'updateStr' => $result['updateStr'],
                        'tel' => $result['tel'],
                        'expSpellName' => $result['expSpellName'],
                        'expTextName' => $result['expTextName'],
                        'data' => value($result, 'data')
                    );

                    break;
                } else {
                    // 失败
                    $send['error'] = !empty($result['msg']) ? $result['msg'] : '暂无跟踪信息';
                }
            } else {
                $send['error'] = '查询失败';
            }
        } while(false);
        $this->output->send($send);
    }
}